//
//  CYCallRecordsTableVC.m
//  CreditAddressBook
//
//  Created by Lee on 15/10/12.
//  Copyright © 2015年 Lee. All rights reserved.
//

#import "CYCallRecordsTableVC.h"
#import "CYTool.h"
#import "CYCallRecord.h"
#import "CYDialTool.h"
#import "CYPerson.h"
#import "CYDatabase.h"

@interface CYCallRecordsTableVC ()

@end

@implementation CYCallRecordsTableVC

- (CGFloat)viewH {
    NSInteger aa = self.callRecords.count;
    return 44 * aa;
}

- (NSArray *)callRecords {
    if (!_callRecords) {
        _callRecords = [NSArray array];
    }
    return _callRecords;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [CYTool setExtraCellLineHiddenWithTableView:self.tableView];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.callRecords = [self loadCallHistory:self.person.phones];
}

#pragma mark - 数据库操作
/**
 *  加载通话记录
 */
- (NSArray *)loadCallHistory:(NSArray *)phones {
    NSMutableArray *calls = [NSMutableArray array];
    for (NSString *phone in phones) {
        NSArray *arr = [[CYDatabase sharedDatabase] callHistorysWithParam:@{@"phone": phone}];
        if (arr && arr.count) {
            [calls addObjectsFromArray:arr];
        }
    }
    return calls;
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.callRecords.count;
//    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    CYCallRecord *call = self.callRecords[indexPath.row];
    cell.textLabel.text = call.newlyCallDate1;
    cell.detailTextLabel.text = call.callHistory.phone;
    cell.detailTextLabel.textColor = [UIColor grayColor];
    
    //通话时长
    UILabel *rightLable = [[UILabel alloc] init];
    rightLable.text = call.callDuration;
    rightLable.font = [UIFont systemFontOfSize:14];
    rightLable.textColor = [UIColor grayColor];
    [cell addSubview:rightLable];
    [rightLable autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:16.f];
    [rightLable autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
    
    UIFont *font = [UIFont systemFontOfSize:14];
    cell.textLabel.font = font;
    cell.detailTextLabel.font = font;
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    CYLog(@"viewH:%f", self.viewH);
//    self.tableView.height = self.viewH;
}

- (NSString *)segmentTitle {
    return @"通话记录";
}

- (UIScrollView *)streachScrollView {
    return self.tableView;
}
@end
